package com.neusoft.neu24.nep.service.impl;

import com.neusoft.neu24.nep.entity.CommonResult;
import com.neusoft.neu24.nep.entity.Order;
import com.neusoft.neu24.nep.mapper.OrderMapper;
import com.neusoft.neu24.nep.service.IOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;


@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements IOrderService {
    @Override
    public CommonResult newOrder(Order order) {
//        http://localhost:8010/a/pay/u001/1
//        wxid aname,price,num
//        获得调用支付服务的路径
        int fee = order.getPrice() * order.getNum();
        String url = "/a/pay/" + order.getWxid() + "/" + fee;
        String basepath = "http://localhost:8010";
// 微服务调用
        CommonResult result = WebClient.create(basepath)
                .put()
                //多个参数也可以直接放到map中,参数名与placeholder对应上即可
//                .uri("http://www.baidu.com/s?wd={key}&other={another}","北京天气","test") //使用占位符
                .uri(url)
                .retrieve()
                .bodyToMono(CommonResult.class)
                .block();

        if (result == null || result.getCode() != 200) {
            return CommonResult.filure();
        }
        order.setFee(fee);
        int i = baseMapper.insert(order);
        return CommonResult.sucess(order.getId());
    }
}
